package be.bonamis.umpire.ui.client.activities;

import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.LoginView;
import be.bonamis.umpire.ui.client.places.LoginPlace;
import be.bonamis.umpire.ui.shared.actions.GetLoginA;
import be.bonamis.umpire.ui.shared.actions.GetLoginR;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.client.OnResponseCommand;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class LoginActivity extends BaseActivity<LoginPlace> implements LoginView.Presenter {

	private LoginView loginView;
	private ActionServiceWrapper asw;

	public LoginActivity(LoginPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
		this.clientFactory = clientFactory;
		this.asw = clientFactory.getActionServiceWrapper();
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		loginView = clientFactory.getLoginView();
		loginView.setPresenter(this);
		panel.setWidget(loginView.asWidget());
	}

	@Override
	public void loginButtonClicked() {		
		asw.execute(new GetLoginA<GetLoginR>(loginView.getLogin().getText(), loginView.getPassword().getText(), loginView
				.getFr().getValue(), loginView.getNl().getValue()), new OnResponseCommand<GetLoginR>() {
			@Override
			public void execute() {
				loginView.getGreetingMessage().setText(response.getGreeting());
			}
		});

	}
}
